<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class MdgDayPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	* adds extra access checks, dependent on page state
	*/
	function page_inc_check_access($page) {
	check_access("READ");
	if (!$_SESSION['user_id'])
		errorpage(6);
	}

	function page_inc_execute_action($page, $action) {
	global $CONFIG;

	switch($action) {
		case "edit":
			if (!$page->id)
				errorpage(9);
			$sqlresult = db_do_query($page, db_query_select(array(array(), "MDAG_GAME.*, nick, GAME.*"), array("(MDAG_GAME INNER JOIN MEMBER ON dm = user_id) INNER JOIN GAME ON MDAG_GAME.game_id = GAME.game_id", "DUMMY"), "mdag_game_id = {$page->id}"));
			if (db_num_rows($sqlresult) != 1)
				errorpage(404);
			$mgame = db_fetch_array($sqlresult);
			if ($mgame['dm'] != $_SESSION['user_id'])
				errorpage(5);

		case "add":
				
			if ($_POST['submitted']) {
			
				$checker = new InputChecker();
				$ok = ($checker->check_input($_POST['game_id'] != "TYPE", "Kies een spel (je koos een spelcategorie)") and 
					   $checker->check_exists($_POST['title'], "naam") and 
					   $checker->check_tags($_POST['title'], "naam") and 
					   $checker->check_exists($_POST['info'], "beschrijving") and 
					   $checker->check_tags_advanced($_POST['info'], "beschrijving") and 
					   $checker->check_tag_closures($_POST['info'])); 
							
				if ($ok) {
				
					if ($_GET['action'] == "add") { // add group
						db_do_query($page, db_query_insert(array('dm', 'info', 'title', 'open', 'game_id'), array($_SESSION['user_id'], db_sqlize($_POST['info']), db_sqlize($_POST['title']), 1, $_POST['game_id']), "MDAG_GAME"));
						$mgame_id = db_insert_id();
					} else { // update group
						db_do_query($page, db_query_update(array('info', 'title', 'open', 'game_id'), array(db_sqlize($_POST['info']), db_sqlize($_POST['title']), ($_POST['open'] > 0) ? 1 : 0, $_POST['game_id']), "MDAG_GAME", "mdag_game_id = {$mgame['mdag_game_id']}"));
						$mgame_id = $mgame['mdag_game_id'];
					}
					
					endpage("OK, de gegevens zijn verwerkt!<script language=JavaScript>setTimeout(\"location.href='/home/conventies/{$mgame_id}.mdag'\", 1500);</script>");
				}
			}
			
			$inputs = array();
			
			$creator = new FormCreator();
			$inputs[] = $creator->header("Een spel leiden op mandragondag 6");
			$inputs[] = $creator->input_field("Titel", "title", $mgame['title']);
			
			$manager = new GameManager();
			$game_kv = $manager->game_get_games_kv();
			$inputs[] = $creator->input_select("Spel", "game_id", $game_kv['keys'], $game_kv['values'], "", $mgame['game_id']);
			
			$inputs[] = $creator->input_text("Beschrijving", "info", stripslashes($mgame['info']));
			
			if ($_GET['action'] == "edit")
				$inputs[] = $creator->input_select("Inschrijvingen", "open", array(1,-1), array('open', 'afgesloten'));
			
			$page->content['form'] = $creator->create('mdagform', array_reverse($inputs));

			break;
		case "subscribe":
			db_do_query($page, db_query_insert_multiple(array('mdag_game_id', 'user_id'), array(array($page->id, $_SESSION['user_id'])), "MDAG_SUBSCR", 1));
			$this->alerter->add_alert("OK, ingeschreven voor dit spel");
			break;
		case "unsubscribe":
			db_do_query($page, db_query_delete("MDAG_SUBSCR", "mdag_game_id = {$page->id} AND user_id = {$_SESSION['user_id']}"));
			$this->alerter->add_alert("OK, uitgeschreven voor dit spel");
			break;
		default:
			$this->alerter->add_alert("unknown action: $action");
			break;
	}
	}

	/**
	* execute page logic
	*/
	function page_inc_execute($page) {

	if ($page->id) {
		$sqlresult = db_do_query($page, db_query_select(array(array(), "MDAG_GAME.*, nick, GAME.*"), array("(MDAG_GAME INNER JOIN MEMBER ON dm = user_id) INNER JOIN GAME ON MDAG_GAME.game_id = GAME.game_id", "DUMMY"), "mdag_game_id = {$page->id}"));
		if (db_num_rows($sqlresult) != 1)
			errorpage(404);
		$mgame = db_fetch_array($sqlresult);
		if (db_num_rows(db_do_query($page, db_query_select(array(array('*')), array("MDAG_SUBSCR"), "user_id = {$_SESSION['user_id']} AND mdag_game_id = {$page->id}"))))
			$mgame['subscr'] = 1;
	}

	if (!$_GET['action']) {

		if ($page->id) {
			$text = "<p>Spelleider: {$mgame['nick']}\n<p>" . (stripslashes($mgame['info'])) . "\n<p>Inschrijvingen voor dit spel zijn " . ($mgame['open'] ? "open" : "afgesloten") . ($mgame['link'] ? "\n<p>Meer over <a href={$mgame['link']}>{$mgame['name']}</a>" : "") . "\n\n<p>Spelers:\n<ul>";
		
			$sqlresult = db_do_query($page, db_query_select(array(array(), "nick, MEMBER.user_id"), array("MEMBER INNER JOIN MDAG_SUBSCR ON MEMBER.user_id = MDAG_SUBSCR.user_id", "DUMMY"), "mdag_game_id = {$mgame['mdag_game_id']}"));
			while ($r = db_fetch_array($sqlresult))
				$text .= "\n<li><a href=/home/profiles/{$r['user_id']}.profile>{$r['nick']}</a>";
		
			$text .= "\n</ul>";
		} else {

			$sqlresult = db_do_query($page, db_query_select(array(array(), "MDAG_GAME.*, nick, GAME.*"), array("(MDAG_GAME INNER JOIN MEMBER ON dm = user_id) INNER JOIN GAME ON MDAG_GAME.game_id = GAME.game_id", "DUMMY"), 1, "name ASC"));
			$text = "<ul>";
			while ($r = db_fetch_array($sqlresult))
				$text .= "<li>{$r['nick']} organiseert '<a href=/home/conventies/{$r['mdag_game_id']}.mdag>" . stripslashes($r['title']) . "</a>' ({$r['name']}) " . ($r['open'] ? "" : " (inschrijvingen gesloten)");
			$text .= "</ul>";

		}
	}

	add_action('mdag-add', 'url-action', 'add');
	if ($page->id) {

		if ($_SESSION['user_id'] == $mgame['dm'])
			add_action('mdag-edit', 'url-action', 'edit');

		if ($mgame['subscr'])
			add_action('mdag-unsubscribe', 'url-action', 'unsubscribe');
		else
			add_action('mdag-subscribe', 'url-action', 'subscribe');
			
	}
						
	$page->content['body'] = trim($text) ? $text : "<p>Gebruik het actiemenu rechts boven om een spel toe te voegen</p>";

	$page->content['title'] = $page->id ? stripslashes($mgame['title']) : "MandragonDAG VIII";
	}

	/**
	* dress up page content using skin stuff
	*/
	function page_inc_dress($page) {
	print_body($page);
	if (($_GET['action'] == "edit") or ($_GET['action'] == "add"))
		print_form($page, $page->content['form']);
	}
}

?>
